using System;
using System.Collections.Generic;
using System.Text;

namespace TelnetExpect
{
    /// <summary>
    /// Telnet Option Handler base class.
    /// </summary>
    public abstract class TelnetOptionHandler
    {
        TelnetStream m_telnet;
        bool m_gotData;

        //----
        protected TelnetOptionHandler()
        {
        }

        /// <summary>
        /// When overridden in a subclass
        /// initializes the instance -- as now the parent is set etc.
        /// </summary>
        protected internal virtual void Init()
        {
        }

        //----
        internal void SetParent(TelnetStream telnet)
        {
            m_telnet = telnet;
        }

        /// <summary>
        /// Gets the parent <see cref="T:TelnetExpect.TelnetStream"/> instance.
        /// </summary>
        protected TelnetStream Telnet { get { return m_telnet; } }

        //----
        internal bool DoHandleSubNegotiation(byte[] data)
        {
            m_gotData = false;
            HandleSubNegotiation(data);
            return m_gotData;
        }

        /// <summary>
        /// When overridden in a subclass
        /// handles receipt of Telnet SubNegotiation command.
        /// </summary>
        /// -
        /// <param name="data">The content of the SubNegotiation command.
        /// </param>
        protected virtual void HandleSubNegotiation(byte[] data)
        {
        }

        //--
        internal void DoOnChangedState(bool local, TelnetOption option, bool newState, bool already, TelnetBaseStream.Command command)
        {
            OnChangedState(local, option, newState, already, command);
        }

        /// <summary>
        /// When overridden in a subclass
        /// handles an Option having changed state.
        /// </summary>
        /// -
        /// <param name="local">Whether the changed option is the local or remote
        /// instance..
        /// </param>
        /// <param name="option">The changed Option id.
        /// </param>
        /// <param name="newState">A <see cref="T:System.Boolean"/> indicating
        /// the new state of the option.
        /// </param>
        protected virtual void OnChangedState(bool local, TelnetOption option, bool newState, bool already, TelnetBaseStream.Command command)
        {
        }

        //----
        protected void AddResponseSubNegotiation(byte[] response)
        {
            m_telnet.AddResponseSubNegotiation(response);
            m_gotData = true;
        }

        //----
        /// <summary>
        /// Writes diagnostic information.
        /// </summary>
        /// -
        /// <param name="message">The descriptive message.
        /// </param>
        protected void TraceWriteLine(string message)
        {
            m_telnet.TraceWriteLine(message);
        }

        /// <summary>
        /// Writes diagnostic information.
        /// </summary>
        /// -
        /// <param name="format">The format string.
        /// </param>
        /// <param name="args">The arguments used by the format string.
        /// </param>
        protected void TraceWriteLine(string format, params object[] args)
        {
            m_telnet.TraceWriteLine(format, args);
        }

        /// <summary>
        /// Writes diagnostic information about the content of a buffer.
        /// </summary>
        /// -
        /// <param name="prefixMessage">The descriptive message to prefix the buffer information.
        /// </param>
        /// <param name="buffer">An array of bytes containing the buffer content.
        /// </param>
        /// <param name="offset">The point at which the content begins in the byte array.
        /// </param>
        /// <param name="count">The number of bytes of content in the array.
        /// </param>
        protected void TraceWriteLine(string prefixMessage, byte[] buffer, int offset, int count)
        {
            m_telnet.TraceWriteLine(prefixMessage, buffer, offset, count);
        }

        /// <summary>
        /// Writes diagnostic information, optionally.
        /// </summary>
        /// -
        /// <param name="condition">A <see cref="T:System.Boolean"/> indicating
        /// whether to write the diagnostic information.
        /// </param>
        /// <param name="message">The descriptive message.
        /// </param>
        protected void TraceWriteLineIf(bool condition, string message)
        {
            m_telnet.TraceWriteLineIf(condition, message);
        }

    }
}
